# main.jl

# This is the main julia script that runs the structural estimation

# Version Summer 2017

#workspace()

cd("C:/Users/johannes.boehm/Dropbox/cenf/data/revision-2017/julia_noise")

root = "C:/Users/johannes.boehm/Dropbox/cenf/data/revision-2017"

include("cenf.jl")

cenf.readData("C:/Users/johannes.boehm/Dropbox/cenf/data/revision-2017")

using JuMP
using KNITRO
using MAT
using JLD2
#using Logging
using Gadfly
using Distributed
using Random

# type Parameters
#   Θ::Float64
#   β::Array{Float64,2}
#   η::Array{Float64,2}
#   logT::Array{Float64,2}
#   logS::Array{Float64,2}
# end

# First start with randomly selected points for Θ,β,η, then pick the best one, and do a bilevel optimization in the neighbourhood.


# --------------------------------------
# Z_GM
# --------------------------------------


#println("Starting z_gm  (fval/result)")

# Get more workers
workers = addprocs(8; exeflags="--project")

# include("do_zgm_fixedtheta.jl")
# include("do_zgm_variabletheta.jl")

# --------------------------------------
# Z_F
# --------------------------------------

#println("Starting z_f  (fval/result)")

include("do_zf.jl")

# include("do_zf_variabletheta.jl")
# include("do_zf_fixedtheta.jl")

rmprocs(workers)

# --------------------------------------
# PRINT TABLE WITH ESTIMATION RESULTS
# --------------------------------------
include("estimationResults.jl")

# --------------------------------------
# SIMULATE WELFARE COUNTERFACTUALS
# --------------------------------------

# (ordering of the paper text)
# first approach: hold theta fixed, estimate each d() semiparametrically
include("simulateCounterfactuals_fixedtheta.jl")
# second approach: use functional form from microfoundation, estimate theta
include("simulateCounterfactuals_variabletheta.jl")

# put an ols regression line through the dots
#using DataFrames, GLM
#df = DataFrame(delta=cenf.δ[:,1,1], us_fgm=changeArray_FGM[:,1],us_ff=changeArray_FGM[:,1],us_vgm=changeArray_FGM[:,1],us_vf=changeArray_FGM[:,1] )
#OLS_fgm = glm(us_fgm ~ delta, df, Normal(), IdentityLink())

# --------------------------------------
# CES ESTIMATION AND COUNTERFACTUALS
# --------------------------------------

# estimation
include("do_ces_zgm_rhothetafixed.jl")
include("do_ces_zf_rhothetafixed.jl")
# counterfactuals
include("simulateCounterfactuals_ces.jl")


# produce a latex table that shows the welfare effects
# produce core of a latex table that shows the results
open("welfaretable.tex","w") do fhh
    for i=1:109
      write(fhh, @sprintf("%s & %s & %0.2f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f \\\\ \n", cenf.countrynames_full[i],cenf.countrynames[i],cenf.δ[i,1,1],100*changeArray_FGM[i,1],100*changeArray_FGM[i,2],100*changeArray_FF[i,1],100*changeArray_FF[i,2],100*changeArray_VGM[i,1],100*changeArray_VGM[i,2],100*changeArray_VF[i,1],100*changeArray_VF[i,2]))
        # cenf.countrynames_full[i]
        # cenf.countrynames[i],cenf.δt[i,1,1]
        # 100*changeArray_FGM[i,1]
        # 100*changeArray_FGM[i,2]
        # 100*changeArray_FF[i,1]
        # 100*changeArray_FF[i,2]
        # 100*changeArray_VGM[i,1]
        # 100*changeArray_VGM[i,2]
        # 100*changeArray_VF[i,1]
        # 100*changeArray_VF[i,2]
    end
end

open("welfaretable-split1.tex", "w") do fhh
  for i=1:59
    write(fhh, @sprintf("%s & %s & %0.2f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f \\\\ \n", cenf.countrynames_full[i],cenf.countrynames[i],cenf.δ[i,1,1],100*changeArray_FGM[i,1],100*changeArray_FGM[i,2],100*changeArray_FF[i,1],100*changeArray_FF[i,2],100*changeArray_VGM[i,1],100*changeArray_VGM[i,2],100*changeArray_VF[i,1],100*changeArray_VF[i,2]))
  end
end
open("welfaretable-split2.tex", "w") do fhh
  for i=60:109
    write(fhh, @sprintf("%s & %s & %0.2f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f & %0.1f \\\\ \n", cenf.countrynames_full[i],cenf.countrynames[i],cenf.δ[i,1,1],100*changeArray_FGM[i,1],100*changeArray_FGM[i,2],100*changeArray_FF[i,1],100*changeArray_FF[i,2],100*changeArray_VGM[i,1],100*changeArray_VGM[i,2],100*changeArray_VF[i,1],100*changeArray_VF[i,2]))
  end
end


run(pipeline(`rsvg-convert -h 600 welfare_us_fixedtheta_gm.svg`,stdout="welfare_us_fixedtheta_gm.png"))
run(pipeline(`rsvg-convert -h 600 welfare_us_fixedtheta_f.svg`,stdout="welfare_us_fixedtheta_f.png"))
run(pipeline(`rsvg-convert -h 600 welfare_us_variabletheta_gm.svg`,stdout="welfare_us_variabletheta_gm.png"))
run(pipeline(`rsvg-convert -h 600 welfare_us_variabletheta_f.svg`,stdout="welfare_us_variabletheta_f.png"))

run(pipeline(`rsvg-convert -h 600 welfare_us_ces_gm.svg`,stdout="welfare_us_ces_gm.png"))
run(pipeline(`rsvg-convert -h 600 welfare_us_ces_f.svg` ,stdout="welfare_us_ces_f.png" ))
